package com.turing.post.capitalaccounts.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.SaleCapitalDetail;
import com.turing.post.capitalaccounts.util.SaleCapitalDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "capitalaccounts.impl.SaleCapitalDetailDaoImpl")
@Remote
@Local
public class SaleCapitalDetailDaoImpl implements SaleCapitalDetailDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertSaleCapitalDetail(SaleCapitalDetail obj)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, obj);
	}

	/**
	 * 查询销售机构资金明细列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCapitalDetail> getSaleCapitalDetailList(
			PCPSWebInput webInput, Integer page, Date startTranDate,
			Date endTranDate, BigInteger startAmount, BigInteger endAmount,
			BigInteger ticketId, String saleBranchNo, String tranType) {
		String sql = "select * from SaleCapitalDetail where "; 
		sql += " (tranDate >= to_date(?1,'yyyy-MM-dd hh24:mi:ss') and tranDate <= to_date(?2,'yyyy-MM-dd hh24:mi:ss')) ";
		sql += " and (?3 = -1 or ticketId = ?3) ";
		sql += " and ((?4= '0' and tranType in ('00','10','11','11','02')) or (tranType = ?4)) ";
		sql += " and ((creditDeposit >= ?5 and creditDeposit <= ?6 ) or (creditWithdraw >= ?5 and creditWithdraw <= ?6)) ";
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "saleBranchNo");
		sql += "  order by tranTime desc ";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, SaleCapitalDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		if (startAmount == null) {
			startAmount = Config.startAmount;
		}
		if (endAmount == null) {
			endAmount = Config.endAmount;
		}
		if (startTranDate == null) {
			startTranDate = new Date();
		}
		if (endTranDate == null) {
			endTranDate = new Date();
		}
		query.setParameter(1, PCPSUtil.getPackedYMDString2(startTranDate)
				+ " 00:00:00");
		query.setParameter(2, PCPSUtil.getPackedYMDString2(endTranDate)
				+ " 23:59:59");
		query.setParameter(3, ticketId == null ? new BigInteger("-1")
				: ticketId);
		query.setParameter(4, PCPSUtil.isEmpty(tranType) == true ? "0"
				: tranType);
		query.setParameter(5, startAmount);
		query.setParameter(6, endAmount);
		return query.getResultList();
	}

	/**
	 * 查询销售机构资金明细总数
	 */
	@Override
	public BigInteger getSaleCapitalDetailCount(PCPSWebInput webInput,
			Date startTranDate, Date endTranDate, BigInteger startAmount,
			BigInteger endAmount, BigInteger ticketId, String saleBranchNo,
			String tranType) {
		String sql = "select count(*) from SaleCapitalDetail where ";
		sql += " (tranDate >= to_date(?1,'yyyy-MM-dd hh24:mi:ss') and tranDate <= to_date(?2,'yyyy-MM-dd hh24:mi:ss')) ";
		sql += " and (?3 = -1 or ticketId = ?3) ";
		sql += " and ((?4 = '0' and tranType in ('00','10','11','11','02')) or (tranType = ?4)) ";
		sql += " and ((creditDeposit >= ?5 and creditDeposit <= ?6 ) or (creditWithdraw >= ?5 and creditWithdraw <= ?6)) ";
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "saleBranchNo");
		sql += " order by tranTime desc ";
		Query query = _em.createNativeQuery(sql);
		if (startAmount == null) {
			startAmount = Config.startAmount;
		}
		if (endAmount == null) {
			endAmount = Config.endAmount;
		}
		if (startTranDate == null) {
			startTranDate = new Date();
		}
		if (endTranDate == null) {
			endTranDate = new Date();
		}
		query.setParameter(1, PCPSUtil.getPackedYMDString2(startTranDate)
				+ " 00:00:00");
		query.setParameter(2, PCPSUtil.getPackedYMDString2(endTranDate)
				+ " 23:59:59");
		query.setParameter(3, ticketId == null ? new BigInteger("-1")
				: ticketId);
		query.setParameter(4, PCPSUtil.isEmpty(tranType) == true ? "0"
				: tranType);
		query.setParameter(5, startAmount);
		query.setParameter(6, endAmount);
		return new BigInteger(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCapitalDetail> getCashSaleCapitalDetailList(
			PCPSWebInput webInput, Integer page, Date startTranDate,
			Date endTranDate, BigInteger startAmount, BigInteger endAmount,
			BigInteger ticketId, String saleBranchNo, String tranType) {
		String sql = "select * from SaleCapitalDetail where ";
		sql += "  (tranDate >= to_date(?1,'yyyy-MM-dd hh24:mi:ss') and tranDate <= to_date(?2,'yyyy-MM-dd hh24:mi:ss')) ";
		sql += " and (?3 = -1 or ticketId = ?3) ";
		sql += " and ((?4 = '0' and tranType in ('05','15','06','16')) or (tranType = ?4)) ";
		sql += " and ((cashDeposit >= ?5 and cashDeposit <= ?6 ) or (cashWithdraw >= ?5 and cashWithdraw <= ?6))  ";
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "saleBranchNo");
		sql += "  order by tranTime desc ";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, SaleCapitalDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		if (startAmount == null) {
			startAmount = Config.startAmount;
		}
		if (endAmount == null) {
			endAmount = Config.endAmount;
		}
		if (startTranDate == null) {
			startTranDate = new Date();
		}
		if (endTranDate == null) {
			endTranDate = new Date();
		}
		query.setParameter(1, PCPSUtil.getPackedYMDString2(startTranDate)
				+ " 00:00:00");
		query.setParameter(2, PCPSUtil.getPackedYMDString2(endTranDate)
				+ " 23:59:59");
		query.setParameter(3, ticketId == null ? new BigInteger("-1")
				: ticketId);
		query.setParameter(4, PCPSUtil.isEmpty(tranType) == true ? "0"
				: tranType);
		query.setParameter(5, startAmount);
		query.setParameter(6, endAmount);
		return query.getResultList();
	}

	@Override
	public BigInteger getCashSaleCapitalDetailCount(PCPSWebInput webInput,
			Date startTranDate, Date endTranDate, BigInteger startAmount,
			BigInteger endAmount, BigInteger ticketId, String saleBranchNo,
			String tranType) {
		String sql = "select count(*) from SaleCapitalDetail where ";
		sql += "  tranDate >= to_date(?1,'yyyy-MM-dd hh24:mi:ss') and tranDate <= to_date(?2,'yyyy-MM-dd hh24:mi:ss') ";
		sql += " and (?3 = -1 or ticketId = ?3) ";
		sql += " and ((?4 = '0' and tranType in ('05','15','06','16')) or (tranType = ?4)) ";
		sql += " and ((cashDeposit >= ?5 and cashDeposit <= ?6 ) or (cashWithdraw >= ?5 and cashWithdraw <= ?6))  ";
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "saleBranchNo");
		sql += "  order by tranTime desc ";
		Query query = _em.createNativeQuery(sql);
		if (startAmount == null) {
			startAmount = Config.startAmount;
		}
		if (endAmount == null) {
			endAmount = Config.endAmount;
		}
		if (startTranDate == null) {
			startTranDate = new Date();
		}
		if (endTranDate == null) {
			endTranDate = new Date();
		}
		query.setParameter(1, PCPSUtil.getPackedYMDString2(startTranDate)
				+ " 00:00:00");
		query.setParameter(2, PCPSUtil.getPackedYMDString2(endTranDate)
				+ " 23:59:59");
		query.setParameter(3, ticketId == null ? new BigInteger("-1")
				: ticketId);
		query.setParameter(4, PCPSUtil.isEmpty(tranType) == true ? "0"
				: tranType);
		query.setParameter(5, startAmount);
		query.setParameter(6, endAmount);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 按销售机构号分类汇总某日某销售商下属销售机构资金明细
	 * 
	 * @param issueCompanyNo
	 *            发卡商NO
	 * @param tranDate
	 *            帐务日期
	 * @param saleCompanyNo
	 *            销售商号
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> sumAllSaleCapitalDaySettlementByBranch(
			String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("select t.saleBranchNo, "
						+ "sum(t.creditDeposit), sum(t.creditWithdraw), sum(t.cashDeposit), sum(t.cashWithdraw) "
						+ "from SaleCapitalDetail t "
						+ "where t.saleBranchNo like ?1 and t.tranDate = ?2 "
						+ "group by t.saleBranchNo ");
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		List<Object[]> obj_list = query.getResultList();
		if (obj_list == null)
			return null;
		return obj_list;
	}

	/**
	 * 汇总指定机构现金上缴业务
	 * 
	 * @param saleBranchNo
	 * @param tranDate
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> getBranchDaySettByBranchAndTranDate(
			String saleBranchNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("select t.saleBranchNo, "
						+ "count(*), sum(t.cashWithdraw) "
						+ "from SaleCapitalDetail t "
						+ "where t.saleBranchNo = ?1 and t.tranDate = ?2 and t.tranType in ('06','16') "
						+ "group by t.saleBranchNo ");
		query.setParameter(1, saleBranchNo);
		query.setParameter(2, tranDate);
		List<Object[]> obj_list = query.getResultList();
		if (obj_list == null)
			return null;
		return obj_list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCapitalDetail> getAllSaleCapitalDetailListByTicketId(
			BigInteger ticketId, String tranType) {
		String sql = "select * from SaleCapitalDetail where TicketId=?1 and TranType=?2";
		Query query = _em.createNativeQuery(sql, SaleCapitalDetail.class);
		query.setParameter(1, ticketId);
		query.setParameter(2, tranType);
		return query.getResultList();
	}
}
